#include "D3D9Engine.h"


void CD3D9Engine::ClearBuffers()
{
	if(_d3dDev != NULL)
		_d3dDev->Clear(0, NULL, D3DCLEAR_TARGET, XRGB(128, 196, 255), 1.0f, 0);
}


void CD3D9Engine::BeginScene(bool clear, int lockFrame)
{
	if(_d3dDev == NULL)
		return;

	HRESULT hr = 0;

	hr = _d3dDev->TestCooperativeLevel();

	if(hr == D3DERR_DEVICELOST)
	{
		Sleep(500);
		this->OnFreeResources();

	}
	else if(hr == D3DERR_DEVICENOTRESET)
	{
		hr = _d3dDev->Reset(&_pp);

		if(SUCCEEDED(hr))
		{
			this->OnInitResources();
		}
	}

	if(clear)
	{
		this->ClearBuffers();
	}

	_d3dDev->BeginScene();

	this->_isSceneBegun = true;
}


void CD3D9Engine::EndScene()
{
	if(_d3dDev == NULL)
		return;

	_d3dDev->EndScene();
	_d3dDev->Present(NULL, NULL, NULL, NULL);

	this->_isSceneBegun = false;
}


void CD3D9Engine::Render(IRenderable* renderable, D3DXMATRIX* transform)
{
	if(_d3dDev == NULL || renderable == NULL)
		return;


	renderable->Render(_d3dDev, transform);
}